<template>
  <div class="app-container">
    <!-- <eHeader :query="query"/> -->
    <div class="head-container">
    <!-- 搜索 -->
      <el-input v-model="params.key" clearable placeholder="输入关键词搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="handleQuery"/>
      <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
      <el-button v-permission="['ADMIN','REDIS_ALL','REDIS_DELETE']" :loading="deleteAllLoading" type="warning" size="mini"  icon="el-icon-delete" @click="deleteAll">清空缓存</el-button>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" :height="tableHeight" size="mini" border style="width: 100%;">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <div>{{ scope.$index + 1 }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip header-align="center" prop="key" label="KEY" width="150px"/>
      <el-table-column prop="value" label="VALUE" header-align="center">
        <template slot-scope="scope">
          <div style="word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
            {{ scope.row.value }}
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['admin'])" label="操作" width="60px" align="center">
        <template slot-scope="scope">
          <el-button slot="reference" type="text" size="mini" @click="handleDelete(scope.row)">
            <i class="el-icon-delete" style="color:red"  title="删除"/>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :page-size="20"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import tableHeight from '@/mixins/tableHeight'
import initData from '@/mixins/initData'
import { del, delAll } from '@/api/base/redis'
export default {
  mixins: [initData, tableHeight],
  data () {
    return {
      delLoading: false,
      sup_this: this,
      permissions: [],
      deleteAllLoading: false
    }
  },
  created () {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    beforeInit () {
      this.url = 'api/monitor/redis/page'
      this.page = 0
      if (!this.params.key) {
        this.params.key = '*'
      }
      return true
    },
    handleQuery () {
      this.page = 0
      this.init()
    },
    handleDelete (row) {
      this.delLoading = true
      del(row.key).then(res => {
        this.delLoading = false
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        console.log(err.response.data.message)
      })
    },
    deleteAll () {
      this.deleteAllLoading = true
      delAll().then(res => {
        this.page = 0
        this.init()
        this.deleteAllLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
